﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Admin1_Default2 : System.Web.UI.Page
{
    //Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {

        ImageButton4.Attributes.Add("onclick", "javascript:return window.confirm('您确定要删除选中项吗?')");
        if (!IsPostBack)
        {
            if (Session["ID"] == null)
            {
                Response.Write("<script>alert('非法登录！');location='../admin.aspx'</script>");
            }
            else
            {
                //dataconn.bindinfostring(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "'", "c_name" );
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    GridView1.DataSource = from q in ds.Course orderby q.c_id descending select q;
                    GridView1.DataBind();
                }
            }
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            if (TextBox1.Text == "")
            {
                //dataconn.bindinfostring(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "'", "c_name");
                GridView1.DataSource = from q in ds.Course orderby q.c_name select q;
                GridView1.DataBind();
            }
            else
            {
                //dataconn.bind(GridView1, "select * from  Course  where  teacher_id='" + Session["ID"] + "'and " + DropDownList1.SelectedValue + " Like '%" + TextBox1.Text + "%'");
                GridView1.DataSource = from q in ds.Course
                                       where q.c_name.Contains(TextBox1.Text.Trim())
                                       orderby q.c_id descending
                                       select q;
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        //dataconn.bindinfostring(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "'", "c_name");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            GridView1.DataSource = from q in ds.Course orderby q.c_id descending select q;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //dataconn.eccom("delete from Course where c_name='" + GridView1.DataKeys[e.RowIndex].Value + "'");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);//当前选中记录的主键ID

            //当各种题型的题库里有该课程信息时，不能删除
            int count = 0;
            bool have1 = (from q in ds.SingleProblem
                      join c in ds.Course on q.c_id equals c.c_id
                      where q.c_id == id
                      select q).Any();
            if (have1)
            {
                count = count + 1;
            }
            bool have2 = (from q in ds.MultiProblem
                          join c in ds.Course on q.c_id equals c.c_id
                          where q.c_id == id
                          select q).Any();
            if (have2)
            {
                count = count + 1;
            }
            bool have3 = (from q in ds.JudgeProblem
                          join c in ds.Course on q.c_id equals c.c_id
                          where q.c_id == id
                          select q).Any();
            if (have3)
            {
                count = count + 1;
            }


            if (count == 0)
            {
                var query = (from q in ds.Course
                             where q.c_id == id
                             select q).FirstOrDefault();
                if (query != null)
                {
                    ds.Course.DeleteOnSubmit(query);
                    ds.SubmitChanges();

                    Response.Write("<script>alert('删除成功');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('在题库里已有该课程信息，必须先删除对应题库。');</script>");
            }
        }
        Response.Redirect("ManageCourse.aspx");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        //dataconn.bind(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "' order by c_id DESC");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            GridView1.DataSource = from q in ds.Course orderby q.c_id descending select q;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //SqlConnection cn = dataconn.getcon();
        //cn.Open();
        //int id = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString());
        //string c_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
        //string c_date = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
        //SqlDataReader dr;
        //string sql = "Update Course Set c_name='" + c_name + "',c_date='" + c_date + "'  Where c_id=" + id + "";
        //SqlCommand myCommand = new SqlCommand(sql, cn);
        //dr = myCommand.ExecuteReader();
        //GridView1.EditIndex = -1;
        //cn.Close();
        //dataconn.bind(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "' order by c_id DESC");

        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            //int id = int.Parse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString());
            var r = (from q in ds.Course
                     where q.c_id == Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value) //id
                     select q).FirstOrDefault();
            if (r != null)
            {
                r.c_name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
                r.c_date = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();

                ds.SubmitChanges();

                GridView1.EditIndex = -1;
                GridView1.DataSource = from q in ds.Course orderby q.c_id descending select q;
                GridView1.DataBind();
            }

        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        //dataconn.bind(GridView1, "select * from Course where teacher_id='" + Session["ID"] + "' order by c_id DESC");
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            GridView1.DataSource = from q in ds.Course orderby q.c_id descending select q;
            GridView1.DataBind();
        }
    }
    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            if (ch.Checked == true)
            {

                ch.Checked = false;

            }
            else
            {

                ch.Checked = true;

            }

        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            ch.Checked = false;

        }
    }
    protected void ImageButton4_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            int count = 0;

            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
                if (ch.Checked == true)
                {
                    //dataconn.eccom("delete from Course where c_id='" + GridView1.DataKeys[gr.RowIndex].Value + "'");

                    //当各种题型的题库里有该课程信息时，不能删除
                    var rd1 = from q in ds.SingleProblem
                              where q.c_id == Convert.ToInt32(GridView1.Rows[gr.RowIndex].Cells[1])
                              select q;
                    if (rd1 != null)
                    {
                        count = count + 1;
                    }
                    var rd2 = from q in ds.MultiProblem
                              where q.c_id == Convert.ToInt32(GridView1.Rows[gr.RowIndex].Cells[1])
                              select q;
                    if (rd2 != null)
                    {
                        count = count + 1;
                    }
                    var rd3 = from q in ds.JudgeProblem
                              where q.c_id == Convert.ToInt32(GridView1.Rows[gr.RowIndex].Cells[1])
                              select q;
                    if (rd3 != null)
                    {
                        count = count + 1;
                    }

                    if (count == 0)
                    {
                        var query = (from q in ds.Course
                                     where q.c_id == Convert.ToInt32(GridView1.DataKeys[gr.RowIndex].Value)
                                     select q).FirstOrDefault();
                        if (query != null)
                        {
                            ds.Course.DeleteOnSubmit(query);
                            ds.SubmitChanges();
                        }
                    }
                }
            }
            if (count==0)
            {
                Response.Write("<script>alert('删除成功');</script>");
            }
            else
            {
                Response.Write("<script>alert('在题库里已有该课程信息，必须先删除对应题库。');</script>");     
            }
            Response.Redirect("ManageStudent.aspx");
        }
    }

}